package com.fuxi.cloud.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.fuxi.cloud.common.constant.CommonConstant;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * ExcelUtil
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class ExcelUtil {
    public ExcelUtil() {
    }

    public static boolean isHave(String[] strs, String s) {
        for(int var2 = 0; var2 < strs.length; ++var2) {
            if (oConvertUtils.isNotEmpty(s) && s.contains(strs[var2])) {
                return true;
            }
        }

        return false;
    }

    public static Map<String, Object> isExistence(List<String> list, String[] text, String[] symbols, Integer n) {
        HashMap var4 = new HashMap();

        try {
            String var5 = (String)list.get(n);
            String var6 = ifHaveEnglish(text, var5);
            String var7 = ifHaveSysbom(symbols, var5);
            if (oConvertUtils.isEmpty(var7)) {
                return null;
            } else if (!var5.contains(var6)) {
                return null;
            } else {
                String var8 = var5.replace(var6, "");
                String[] var9 = var7.split("__");
                String[] var10 = var9;
                int var11 = var9.length;

                int var12;
                for(var12 = 0; var12 < var11; ++var12) {
                    String var13 = var10[var12];
                    var8 = var8.replace(var13, "");
                }

                String var15 = var8.replaceAll("[^A-Z]", "");
                String var16 = "";

                for(var12 = 0; var12 < var15.length(); ++var12) {
                    var16 = var16 + var15.charAt(var12) + ",";
                }

                var4.put("newFirstWord", var16.substring(0, var16.lastIndexOf(",")));
                var4.put("english", var6);
                var4.put("sysbom", var7);
                return var4;
            }
        } catch (Exception var14) {
            return null;
        }
    }

    public static String ifHaveSysbom(String[] strs, String s) {
        String var2 = "";

        for(int var3 = 0; var3 < strs.length; ++var3) {
            if (s.contains(strs[var3])) {
                var2 = var2 + strs[var3] + "__";
            }
        }

        if (oConvertUtils.isNotEmpty(var2)) {
            var2 = var2.substring(0, var2.lastIndexOf("__"));
        }

        return var2;
    }

    public static String ifHaveEnglish(String[] strs, String s) {
        for(int var2 = 0; var2 < strs.length; ++var2) {
            if (s.contains(strs[var2].split("=")[1])) {
                return strs[var2].split("=")[1];
            }
        }

        return "";
    }

    public static String getCellValueByCell(Cell cell) {
        if (cell != null && !cell.toString().trim().equals("")) {
            String var1 = "";
            CellType var2 = cell.getCellTypeEnum();
            switch(var2) {
                case STRING:
                    var1 = cell.getStringCellValue();
                    break;
                case BOOLEAN:
                    var1 = String.valueOf(cell.getBooleanCellValue());
                    break;
                case NUMERIC:
                    var1 = String.valueOf(cell.getNumericCellValue());
                    break;
                case FORMULA:
                    var1 = String.valueOf(cell.getCellFormula());
                    break;
                default:
                    var1 = "";
            }

            return var1;
        } else {
            return "";
        }
    }

    public static String isHaveText(String[] strs, String s) {
        for(int var2 = 0; var2 < strs.length; ++var2) {
            if (oConvertUtils.isNotEmpty(s) && s.contains(strs[var2])) {
                return strs[var2];
            }
        }

        return "";
    }

    public static SimpleDateFormat getFormat(String dataText) {
        SimpleDateFormat var1 = null;
        SimpleDateFormat var2 = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat var3 = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat var4 = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat var5 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        byte var7 = -1;
        switch(dataText.hashCode()) {
            case 3076014:
                if (dataText.equals("date")) {
                    var7 = 0;
                }
                break;
            case 3560141:
                if (dataText.equals("time")) {
                    var7 = 2;
                }
                break;
            case 95356484:
                if (dataText.equals("date2")) {
                    var7 = 1;
                }
                break;
            case 1793702779:
                if (dataText.equals("datetime")) {
                    var7 = 3;
                }
        }

        switch(var7) {
            case 0:
                var1 = var2;
                break;
            case 1:
                var1 = var3;
                break;
            case 2:
                var1 = var4;
                break;
            case 3:
                var1 = var5;
                break;
            default:
                var1 = null;
        }

        return var1;
    }

    public static SimpleDateFormat getDataFormat(String text) {
        SimpleDateFormat var1 = null;
        if (text.contains("-") && text.contains(":")) {
            var1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (text.contains("-")) {
            var1 = new SimpleDateFormat("yyyy-MM-dd");
        } else if ((!isHave(CommonConstant.ABBREVIATION, text) || !text.contains("PM")) && !text.contains("AM")) {
            if ((!text.contains(":") || !text.contains("PM")) && !text.contains("AM")) {
                if (text.contains(":")) {
                    var1 = new SimpleDateFormat("HH:mm:ss");
                } else if (isHave(CommonConstant.ABBREVIATION, text)) {
                    var1 = new SimpleDateFormat("MMM d, yyyy", Locale.ENGLISH);
                }
            } else {
                var1 = new SimpleDateFormat("K:m:s a", Locale.ENGLISH);
            }
        } else {
            var1 = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
        }

        return var1;
    }

    public static byte[] getImage(String text, String lineStart, Integer width, Integer height) throws FileNotFoundException, IOException {
        byte var4 = 4;
        String[] var5 = text.split("\\|");
        int var6 = var5.length - 1;
        if (var6 > 4) {
            return null;
        } else {
            BufferedImage var7 = new BufferedImage(width, height, var4);
            Graphics2D var8 = (Graphics2D)var7.getGraphics();
            var8.setColor(Color.WHITE);
            var8.fillRect(0, 0, width, height);
            var8.setColor(Color.BLACK);
            float var9 = 1.0F;
            var8.setStroke(new BasicStroke(var9));
            if (lineStart.equals("lefttop")) {
                formTopToBottom(width, height, var5, var8);
                paintingTopFont(width, height, var5, var8);
            } else {
                formBottomToTop(width, height, var5, var8);
                paintingBottomFont(width, height, var5, var8);
            }

            ByteArrayOutputStream var10 = new ByteArrayOutputStream();
            ImageIO.write(var7, "jpeg", var10);
            return var10.toByteArray();
        }
    }

    public static void formTopToBottom(Integer width, Integer height, String[] split, Graphics2D pen) {
        byte var4 = 0;
        byte var5 = 0;
        int var6 = width;
        int var7 = split.length - 1;
        int var8 = split.length;
        int var9 = height / var7;
        int var10 = width / var8;
        boolean var11 = false;
        int var12 = 0;

        for(int var13 = 1; var13 < split.length; ++var13) {
            int var14 = var9 * var13;
            if (var8 > 2) {
                var14 = var14 + var9 / var7 - 10;
            }

            if (var13 > 1) {
                var14 = height;
            }

            if (var13 == var8 - 1) {
                var6 = var10 * (var8 - var12);
            }

            pen.drawLine(var4, var5, var6, var14);
            ++var12;
        }

    }

    public static void formBottomToTop(Integer width, Integer height, String[] split, Graphics2D pen) {
        byte var4 = 0;
        int var5 = height;
        int var6 = width;
        int var7 = split.length - 1;
        int var8 = split.length;
        int var9 = height / var7;
        int var10 = width / var8;
        int var11 = 0;
        int var12 = 0;

        for(int var13 = 1; var13 < split.length; ++var13) {
            if (var8 > 2) {
                var11 = height - var9 - var9 / var7;
            }

            if (var13 > 1) {
                var11 = 0;
            }

            if (var13 == var8 - 1) {
                var6 = var10 * (var8 - var12);
            }

            pen.drawLine(var4, var5, var6, var11);
            ++var12;
        }

    }

    public static void paintingTopFont(Integer width, Integer height, String[] split, Graphics2D pen) {
        int var4 = split.length - 1;
        int var5 = split.length;
        int var6 = width / var5;
        Font var7;
        double var8;
        double var10;
        if (var5 == 2) {
            pen.setColor(Color.BLACK);
            var7 = new Font("Microsoft YaHei", 0, 12);
            pen.setFont(var7);
            var8 = (double)height * 0.3D;
            pen.drawString(split[1], var6, (int)var8);
            var10 = (double)height * 0.8D;
            pen.drawString(split[0], 4, (int)var10);
        } else if (var5 == 3) {
            pen.setColor(Color.BLACK);
            var7 = new Font("Microsoft YaHei", 0, 12);
            pen.setFont(var7);
            var8 = (double)height * 0.3D;
            var10 = (double)width / 1.5D;
            pen.drawString(split[2], (int)var10, (int)var8);
            pen.setColor(Color.BLACK);
            double var12 = (double)height * 0.9D;
            pen.drawString(split[1], (int)var10, (int)var12);
            double var14 = (double)height * 0.8D;
            pen.drawString(split[0], 4, (int)var14);
        }

    }

    public static void paintingBottomFont(Integer width, Integer height, String[] split, Graphics2D pen) {
        int var4 = split.length - 1;
        int var5 = split.length;
        int var6 = width / var5;
        Font var7;
        double var8;
        double var10;
        if (var5 == 2) {
            pen.setColor(Color.BLACK);
            var7 = new Font("Microsoft YaHei", 0, 12);
            pen.setFont(var7);
            var8 = (double)height * 0.8D;
            pen.drawString(split[1], var6, (int)var8);
            pen.setColor(Color.BLACK);
            var10 = (double)height * 0.4D;
            pen.drawString(split[0], 4, (int)var10);
        } else if (var5 == 3) {
            pen.setColor(Color.BLACK);
            var7 = new Font("Microsoft YaHei", 0, 12);
            pen.setFont(var7);
            var8 = (double)height * 0.4D;
            pen.drawString(split[2], 4, (int)var8);
            pen.setColor(Color.BLACK);
            var10 = (double)height * 0.24D;
            double var12 = (double)width / 1.5D;
            pen.drawString(split[1], (int)var12, (int)var10);
            double var14 = (double)height * 0.8D;
            pen.drawString(split[0], (int)var12, (int)var14);
        }

    }

    public static void paintLine(Drawing drawing, ClientAnchor anchor, String lineType, String color) {
        XSSFDrawing var4 = (XSSFDrawing)drawing;
        XSSFClientAnchor var5 = (XSSFClientAnchor)anchor;
        XSSFSimpleShape var6 = var4.createSimpleShape(var5);
        var6.setShapeType(1);
        var6.setFillColor(0, 0, 0);
        var6.setLineStyle(0);
        if (ObjectUtil.isNotEmpty(color)) {
            int var7 = Integer.parseInt(color.substring(1, 3), 16);
            int var8 = Integer.parseInt(color.substring(3, 5), 16);
            int var9 = Integer.parseInt(color.substring(5, 7), 16);
            var6.setLineStyleColor(var7, var8, var9);
        } else {
            var6.setLineStyleColor(0, 0, 0);
        }

        if (!lineType.equals("lefttop")) {
            var6.getCTShape().getSpPr().getXfrm().setFlipV(true);
        }

        var6.setLineWidth(1.0D);
    }
}
